package device

import (
	"gitee.com/xlizy/common-go/base/enums"
	"strconv"
)

type Device int

const (
	Other Device = -1
	Web   Device = iota
	Wap
	Ios
	Android
	WechatBrowser
	WechatMiniProgram
)

func (e Device) Code() int32 {
	return int32(e)
}

func (e Device) Des() string {
	return enums.BE(e).Des
}

func (e Device) Enum() string {
	return enums.BE(e).Enum
}

func (e Device) Val() int {
	return int(e)
}

func (e *Device) UnmarshalJSON(value []byte) error {
	*e = Device(enums.UnmarshalEnum(value))
	return nil
}

func (e Device) MarshalJSON() ([]byte, error) {
	switch e {
	case Other:
		return enums.JsonObj(int(e), "Other", "未知")
	case Web:
		return enums.JsonObj(int(e), "Web", "Web")
	case Wap:
		return enums.JsonObj(int(e), "Wap", "Wap")
	case Ios:
		return enums.JsonObj(int(e), "Ios", "Ios")
	case Android:
		return enums.JsonObj(int(e), "Android", "Android")
	case WechatBrowser:
		return enums.JsonObj(int(e), "WechatBrowser", "微信浏览器")
	case WechatMiniProgram:
		return enums.JsonObj(int(e), "WechatMiniProgram", "微信小程序")
	}
	return []byte(strconv.Itoa(int(e))), nil
}
